package com.logic.modular.act.playerInfo;

import cn.hutool.core.bean.BeanUtil;
import com.comment.util.message.ResponseResult;
import com.logic.comment.util.JwtUtils;

import com.logic.modular.act.playerInfo.vo.PlayerInfoVo;
import com.comment.pojo.domain.logic.domain.Player;
import com.comment.pojo.domain.logic.domain.PlayerStatus;
import com.logic.modular.game.player.info.service.PlayerServiceImpl;
import com.logic.modular.game.player.info.service.PlayerStatusServiceImpl;
import com.logic.modular.game.signIn.service.impl.PlayerSignInServiceImpl;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 游戏账户(Player)表服务控制层
 *
 * @author cxy
 * @since 2024-07-18 10:05:14
 */
@Api(tags = "账号行为")
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/playerAct")
public class PlayerAction {
    @Resource
    private PlayerServiceImpl playerService;


    @Resource
    private PlayerStatusServiceImpl playerStatusService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public ResponseResult<?> login(@RequestHeader String token) {
        Long playerId = JwtUtils.getPlayerId(token);
        String serveKey = JwtUtils.getServeKey(token);
        return ResponseResult.success("登录成功", playerService.login(playerId, serveKey));
    }



    @ApiOperation(value = "更新用户信息")
    @PostMapping("/createUser")
    public ResponseResult<?> createUser(
            @RequestHeader String token,
            @Valid @RequestBody PlayerInfoVo.PlayerVo.CreateUser param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Player bean = BeanUtil.toBean(param, Player.class);
        bean.setUid(playerId);
        boolean b = playerService.updateById(bean);
        if (b) {
            bean = playerService.select(playerId);
            return ResponseResult.success("更新成功", bean);
        } else {
            return ResponseResult.error("更新失败");
        }
    }

    @ApiOperation(value = "自己的用户信息")
    @PostMapping("/getUserInfo")
    public ResponseResult<?> select(@RequestHeader String token) {
        return ResponseResult.success(playerService.select(JwtUtils.getPlayerId(token)));
    }

    @ApiOperation(value = "查询用户信息")
    @PostMapping("/select")
    public ResponseResult<Player> EqSelect(
            @RequestBody PlayerInfoVo.PlayerVo.EqSelect param) {
        return ResponseResult.success(
                playerService.select(param.getUid())
        );
    }

    @ApiOperation(value = "个人行为信息---[比赛,房间]")
    @PostMapping("/ownerActInfo")
    public ResponseResult<PlayerStatus> selectOwner(@RequestHeader String token) {
        return ResponseResult.success("成功获取个人行为信息",
                playerStatusService.selectBy(JwtUtils.getPlayerId(token))
        );
    }

    @ApiOperation(value = "改名")
    @PostMapping("/rename")
    public ResponseResult<?> rename(
            @RequestHeader String token,
            @RequestBody PlayerInfoVo.PlayerVo.rename vo
    ) {
        return ResponseResult.success(playerService.rename(JwtUtils.getPlayerId(token), vo.getNickname()));
    }
}
